import * as types from './mutation-types'
import { deepClone, m7Language, filterXSS } from '@/utils/m7Utils.js'
import { orderBy, isEmpty } from 'lodash'
import Vue from 'vue'

export default {
  [types.UPDATE_WEBCHAT_TODO](state, data) {
    if (isEmpty(data)) {
      return
    }
    const { channelId, key, val } = data
    const channel = state.current.webchat_todo.currentSession[channelId]
    if (isEmpty(channel)) {
      return
    }
    Vue.set(channel, key, val)
    const infoChannel = state.current.webchat_todo.chatInfoById[channelId]
    if (isEmpty(infoChannel)) {
      return
    }
    Vue.set(infoChannel, key, val)
  },
  [types.SET_WEBCHAT_AUTOCLAIM](state, { status }) {
    Vue.set(state, 'isOn', status)
  },
  [types.UPDATE_IMAGE] (state, {chatSessionId, headUrl, sName}) {
    let index = state.webchatList.webchat_todo.list.findIndex(item => item._id == chatSessionId)
    if (index != -1) {
      state.webchatList.webchat_todo.list[index].sName = sName
      state.webchatList.webchat_todo.list[index].headUrl = headUrl
    }
  },
  // [types.SET_END_SESSION_LENGTH] (state, data) {
  //   state.endSessionLength = data
  // },
  [types.SET_WEBCHAT_AUTOCLAIM_OLD] (state, data) {
    Vue.set(state, 'isOnOld', data)
  },
  [types.SET_WEBCHAT_UNDEALNUM] (state, data) {
    Vue.set(state, 'queueNum', data.undealNum)
    if (data.todoNum >= 0) { // fix: todoNum为0时不能正常展示
      Vue.set(state, 'todoNum', data.todoNum)
    }
    if (data.timestamp) {
      Vue.set(state, 'undealNumTimeStamp', data.timestamp)
    }
  },
  [types.QUERY_WEBCHAT_LIST] (state, {webchatList, submenu}) {
    // init为false代表是第一次查询待处理列表
    if (!state.webchatList.webchat_todo.init) {
      Vue.set(state, 'todoNum', webchatList.list.length)
    }
    webchatList.list.forEach((item) => {
      if (item.userStatus === 'online' && item.status === 'finish' && item.platform !== 'weixin' && item.platform !== 'weibo') {
        Vue.set(item, 'userStatus', 'offline')
      }
      if (!item.lastMessage) {
        Vue.set(item, 'lastMessage', {content: '', contentType: '', when: '', type: ''})
      }
    })
    Vue.set(state.webchatList, submenu, webchatList)
    if (submenu === 'webchat_todo') {
      Vue.set(state.webchatList.webchat_todo, 'init', true)
    }
  },
  [types.QUERY_ROBOT_LIST] (state, {webchatList, submenu}) {
    // 查询机器人会话列表
    webchatList.list.forEach((item) => {
      if (item.userStatus === 'online' && item.status === 'finish' && item.platform !== 'weixin' && item.platform !== 'weibo') {
        Vue.set(item, 'userStatus', 'offline')
      }
    })
    Vue.set(state.webchatList, submenu, webchatList)
  },
  [types.POPUP_WEBCHAT_INFO] (state, {session, submenu}) {
    let sourceName = session.sName
    Vue.set(session, 'sourceName', sourceName)
    if (submenu === 'webchat_todo') {
      if (session.invitedUserName === state.currentUser.displayName) {
        Vue.set(session, 'showFunBtn', false)
      } else {
        Vue.set(session, 'showFunBtn', true)
      }
      if (session.status === 'changePeer') {
        Vue.set(session, 'leavemsg', true)
      }
      let preloadObj = {}
      preloadObj.preloadedMsgCon = ''
      preloadObj.preloadedFlag = false
      Vue.set(state.preload, session._id, preloadObj)
      Vue.set(state.current.webchat_todo.currentSession, session._id, session)
    } else {
      Vue.set(state.current[submenu], 'currentSession', session)
    }
  },
  /**
   *  点击进入右侧卡片时 建立messageList 数组 测试途家问题
   */
  [types.INIT_WEBCHAT_MESSAGELIST_SESSION] (state, {sessionId, tabType}) {
    let list = state.current[tabType].messageList[sessionId] || []
    Vue.set(state.current[tabType].messageList, sessionId, list)
  },
  // 更新VueX中对应会话的聊天记录数组
  [types.QUERY_WEBCHAT_HISTORY] (state, {historyList, submenu, sessionId, isCurrentSession, isNew}) {
    // 倒序插入数组
    if (!state.current[submenu].messageList[sessionId]) {
      Vue.set(state.current[submenu].messageList, sessionId, historyList)
    } else {
      if (submenu !== 'webchat_todo' && isNew === true) {
        Vue.set(state.current[submenu].messageList, sessionId, historyList)
      } else {
        state.current[submenu].messageList[sessionId] = historyList.concat(state.current[submenu].messageList[sessionId])
      }
    }
    // 初始化change通知参数
    if (submenu === 'webchat_todo') {
      if (!state.current[submenu].messageListChange[sessionId]) {
        let num = Math.random() + 1
        Vue.set(state.current[submenu].messageListChange, [sessionId], num)
      }
    }
    // 判断是否展示查看更多及是否开启滚动触发
    if (submenu === 'webchat_todo') {
      if (isCurrentSession) {
        if (historyList.length < 10) {
          // 查询的是当前会话记录，且少于10条，展示查询历史的按钮
          Vue.set(state.current[submenu].chatInfoById[sessionId], 'showHistoryBtn', true)
          console.log(state.current[submenu].chatInfoById[sessionId].showHistoryBtn)
        } else {
          // 当前长度等于10条，说明当前会话还有记录可查询，隐藏查询历史的按钮
          Vue.set(state.current[submenu].chatInfoById[sessionId], 'showHistoryBtn', false)
          Vue.set(state.current[submenu].chatInfoById[sessionId], 'finishChatScroll', false)
        }
      } else {
        if (historyList.length < 50) {
          // 查询的是历史会话记录，且少于50条，说明所有记录都已查询完，隐藏查询历史的按钮，禁止滚动查询
          Vue.set(state.current[submenu].chatInfoById[sessionId], 'showHistoryBtn', false)
          Vue.set(state.current[submenu].chatInfoById[sessionId], 'finishChatScroll', true)
        } else {
          // 当前长度等于50条，说明历史会话还有记录可查询，展示查询历史的按钮
          Vue.set(state.current[submenu].chatInfoById[sessionId], 'showHistoryBtn', true)
        }
      }
    } else {
      if (isCurrentSession) {
        if (historyList.length < 10) {
          // 查询的是当前会话记录，且少于10条，展示查询历史的按钮
          Vue.set(state.current[submenu].chatInfoById, 'showHistoryBtn', true)
        } else {
          // 当前长度等于10条，说明当前会话还有记录可查询，隐藏查询历史的按钮
          Vue.set(state.current[submenu].chatInfoById, 'showHistoryBtn', false)
          Vue.set(state.current[submenu].chatInfoById, 'finishChatScroll', false)
        }
      } else {
        if (historyList.length < 50) {
          // 查询的是历史会话记录，且少于50条，说明所有记录都已查询完，隐藏查询历史的按钮，禁止滚动查询
          Vue.set(state.current[submenu].chatInfoById, 'showHistoryBtn', false)
          Vue.set(state.current[submenu].chatInfoById, 'finishChatScroll', true)
        } else {
          // 当前长度等于50条，说明历史会话还有记录可查询，展示查询历史的按钮
          Vue.set(state.current[submenu].chatInfoById, 'showHistoryBtn', true)
        }
      }
    }
  },
  [types.PROCESS_OFFLINE_MSG] (state, {historyList, sessionId, submenu, whenGt}) {
    historyList.reverse()
    if (whenGt) {
      for (let i = historyList.length - 1; i >= 0; i--) {
        state.current[submenu].messageList[sessionId].push(historyList[i])
      }
    } else {
      if (historyList.length > 0) {
        for (let i = historyList.length - 1; i >= 0; i--) {
          state.current[submenu].messageList[sessionId].unshift(historyList[i])
        }
      }
    }
  },
  [types.QUALITY_QUERY_WEBCHAT_HISTORY] (state, {page, historyList}) {
    if (page === 1) {
      Vue.set(state.transCache.qualityCheck, 'messageList', historyList)
    } else {
      for (let i = historyList.length - 1; i >= 0; i--) {
        state.transCache.qualityCheck.messageList.unshift(historyList[i])
      }
      if (historyList.length < 10) {
        Vue.set(state.transCache.qualityCheck, 'finishChatScroll', true)
      }
    }
    Vue.set(state.transCache.qualityCheck, 'page', page + 1)
  },
  [types.QUERY_WEBCHAT_INFO_BY_ID] (state, {chatInfoById, visitInfoList, sessionFormHistory, submenu}) {
    console.log('QUERY_WEBCHAT_INFO_BY_ID')
    if (submenu === 'webchat_todo') {
      if (!state.current[submenu].chatInfoById[chatInfoById._id]) {
        Vue.set(chatInfoById, 'chatScrollPage', 2)
        Vue.set(chatInfoById, 'chatHistoryPage', 1)
        Vue.set(chatInfoById, 'showHistoryBtn', false)
        // Vue.set(chatInfoById, 'waitTime', '')
        // Vue.set(chatInfoById, 'showWaitTime', false)
        Vue.set(chatInfoById, 'beforeTimestamp', 0)
      }
      Vue.set(state.current[submenu].chatInfoById, chatInfoById._id, chatInfoById)
      Vue.set(state.current[submenu].visitInfoList, chatInfoById._id, visitInfoList)
      Vue.set(state.current[submenu].sessionFormHistory, chatInfoById._id, sessionFormHistory)
      Vue.set(state.current[submenu].historyMsgList, chatInfoById._id, [])
      Vue.set(state.current[submenu], 'currentOpenSession', chatInfoById._id)
    } else {
      if (state.current[submenu].chatInfoById._id !== chatInfoById._id) {
        Vue.set(chatInfoById, 'chatScrollPage', 2)
        Vue.set(chatInfoById, 'chatHistoryPage', 1)
        Vue.set(chatInfoById, 'showHistoryBtn', false)
      }
      Vue.set(state.current[submenu], 'historyMsgList', [])
      Vue.set(state.current[submenu], 'chatInfoById', chatInfoById)
      Vue.set(state.current[submenu].sessionFormHistory, chatInfoById._id, sessionFormHistory)
      Vue.set(state.current[submenu], 'visitInfoList', visitInfoList)
    }
  },
  [types.QUERY_WEBCHAT_CURRENT_SESSION] (state, {_id, tabType}) {
    Vue.set(state.current[tabType], 'currentOpenSession', _id)
  },
  [types.GET_CURRENT_USER] (state, {currentUser}) {
    Vue.set(state, 'currentUser', currentUser)
  },
  [types.UPDATE_CURRENT_USER] (state, data) {
    Vue.set(state.currentUser, ['imDealCount'], data.imDealCount)
  },
  [types.LOCATION_CUSTOMER] (state, {customer, submenu, sessionId}) {
    if (submenu === 'webchat_todo') {
      state.current[submenu].currentSession[sessionId].currentCustomer = customer
      Vue.set(state.current[submenu].currentSession[sessionId], 'currentCustomer', customer)
    } else {
      Vue.set(state.current[submenu], 'currentCustomer', customer)
    }
  },
  [types.DELETE_CUSTOMER_IM_RELATION] (state, sessionId) {
    if (state.current.webchat_todo.currentSession[sessionId]) {
      Vue.set(state.current.webchat_todo.currentSession[sessionId], 'currentCustomer', {})
    }
    if (state.current.webchat_all.currentSession._id === sessionId) {
      Vue.set(state.current.webchat_all.currentSession, 'currentCustomer', {})
    }
    if (state.current.webchat_robot.currentSession._id === sessionId) {
      Vue.set(state.current.webchat_robot.currentSession, 'currentCustomer', {})
    }
    if (state.current.webchat_queue.currentSession._id === sessionId) {
      Vue.set(state.current.webchat_queue.currentSession, 'currentCustomer', {})
    }
    if (state.current.webchat_laterProcess.currentSession._id === sessionId) {
      Vue.set(state.current.webchat_laterProcess.currentSession, 'currentCustomer', {})
    }
  },
  [types.DEAL_MSG] (state, {sessionId}) {
    state.webchatList.webchat_todo.list.forEach((item, index) => {
      if (item._id === sessionId) {
        Vue.set(state.webchatList.webchat_todo.list[index], 'leakNum', 0)
      }
    })
  },
  [types.REPLY_MSG] (state, {data, index}) {
    data.dateTime = data.createTime
    data.uidDesc = m7Language('public.myselfI')
    if (data.displayName) {
      data.displayName = decodeURIComponent(data.displayName)
    }
    if (data.sendStatus === 'success') {
      state.current.webchat_todo.messageList[data.sessionId].forEach((item, index) => {
        if (item.timeStamp && item.timeStamp === data.timeStamp) {
          Vue.set(state.current.webchat_todo.messageList[data.sessionId][index], 'showTime', data.dateTime)
          Vue.set(state.current.webchat_todo.messageList[data.sessionId][index], 'sendStatus', data.sendStatus)
          Vue.set(state.current.webchat_todo.messageList[data.sessionId][index], 'when', data.when)
          Vue.set(state.current.webchat_todo.messageList[data.sessionId][index], '_id', data._id)
          Vue.set(state.current.webchat_todo.messageList[data.sessionId][index], 'user', data.user)
          Vue.set(state.current.webchat_todo.messageList[data.sessionId][index], 'dealUserMsg', false)
          if (data.aiContentText) {
            Vue.set(state.current.webchat_todo.messageList[data.sessionId][index], 'aiContentText', data.aiContentText)
          }
        }
      })
    } else {
      state.current.webchat_todo.messageList[data.sessionId].push(data)
      let num = Math.random() + 1
      Vue.set(state.current.webchat_todo.messageListChange, [data.sessionId], num)
      // Vue.set(state.current.webchat_todo.messageList, [data.sessionId], state.current.webchat_todo.messageList[data.sessionId])
    }
    // 更新卡片上最后一条消息内容
    state.webchatList.webchat_todo.list.forEach((item, index) => {
      if (item._id === data.sessionId) {
        let lastMessageWhen = 0
        if (data.when) {
          lastMessageWhen = data.when
        }
        if (state.webchatList.webchat_todo.list[index].lastMessage.content !== undefined) {
          Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'content', decodeURIComponent(data.content))
          Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'contentType', data.contentType)
          Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'lastMessageWhen', lastMessageWhen)
          Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'type', data.type)
        } else {
          Vue.set(state.webchatList.webchat_todo.list[index], 'lastMessage', decodeURIComponent(data.content))
          Vue.set(state.webchatList.webchat_todo.list[index], 'contentType', data.contentType)
          Vue.set(state.webchatList.webchat_todo.list[index], 'lastMessageWhen', lastMessageWhen)
        }
      }
    })
  },
  [types.REPLY_MSG_ERROR] (state, {sessionId, messageId, errCode}) {
    if (state.current.webchat_todo.messageList[sessionId]) {
      state.current.webchat_todo.messageList[sessionId].forEach((item, index) => {
        let itemMessageId = item.messageId ? item.messageId : item._id
        if (itemMessageId === messageId) {
          if (item.platform === 'weixin') {
            Vue.set(state.current.webchat_todo.messageList[sessionId][index], 'weixinres', errCode)
          } else if (item.platform === 'dy') {
            Vue.set(state.current.webchat_todo.messageList[sessionId][index], 'sendRes', errCode)
          } else if (item.platform === 'wxkf') {
            Vue.set(state.current.webchat_todo.messageList[sessionId][index], 'sendRes', errCode)
          } else {
            Vue.set(state.current.webchat_todo.messageList[sessionId][index], 'wxminires', errCode)
          }
        }
      })
    }
  },
  [types.GET_ALL_QUICK_REPLY_LIST] (state, list) {
    Vue.set(state.current.webchat_todo['allQuickReply'], 'list', list)
    Vue.set(state.current.webchat_todo['allQuickReply'], 'flag', false)
  },
  [types.REMOVE_SESSION] (state, id) {
    state.webchatList.webchat_todo.list.forEach((item, index) => {
      if (item._id === id) {
        state.webchatList.webchat_todo.list.splice(index, 1)
        Vue.set(state.webchatList.webchat_todo, 'list', state.webchatList.webchat_todo.list)
        Vue.set(state.webchatList.webchat_todo, 'count', state.webchatList.webchat_todo.list.length)
        Vue.set(state, 'todoNum', state.webchatList.webchat_todo.list.length)
        delete state.current.webchat_todo.currentSession[id]
        delete state.current.webchat_todo.messageList[id]
        delete state.current.webchat_todo.historyMsgList[id]
        delete state.current.webchat_todo.chatInfoById[id]
        delete state.current.webchat_todo.visitInfoList[id]
        delete state.current.webchat_todo.sessionFormHistory[id]
        Vue.set(state, 'refreshWebchatSummary', id)
      }
    })
    if (state.lastContent[id] !== undefined) {
      delete state.lastContent[id]
    }
  },
  [types.CHANGE_INVITE_NOTIFY] (state, {userName, sessionId, sid, toPeer}) {
    Vue.set(state.current.webchat_todo, 'inviteUserName', userName)
    Vue.set(state.current.webchat_todo, 'inviteSessionId', sessionId)
    Vue.set(state.current.webchat_todo, 'sid', sid)
    Vue.set(state.current.webchat_todo, 'toPeer', toPeer)
  },
  [types.SHOW_INVITE_BTN] (state, {flag, sessionId}) {
    Vue.set(state.current.webchat_todo.currentSession[sessionId], 'shwoInviteBtn', flag)
  },
  [types.INVITE_KICK] (state, {sessionId}) {
    Vue.set(state, 'inviteKickNum', Math.random() + 1)
  },
  [types.SET_LOCK_SESSION_STATUS] (state, {flag, sessionId}) {
    Vue.set(state.current.webchat_todo.currentSession[sessionId], 'lockStatus', flag)
  },
  [types.UPDATE_INVITE_INFO] (state, data) {
    Vue.set(state.current.webchat_todo.currentSession[data._id], 'invitedUserName', data.invitedUserName)
    Vue.set(state.current.webchat_todo.currentSession[data._id], 'inviteUserStatus', data.inviteUserStatus)
    Vue.set(state.current.webchat_todo.currentSession[data._id], 'assistAgent', data.assistAgent)
  },
  // 将抢接的会话信息插入webchat_todo list内 然后路由跳转，避免由于后端推送信息延迟造成的报错loading 2018.11.19 by yangxk
  [types.INSERT_GRABSESSION] (state, data) {
    // 修复抢接接口返回比ws推送慢时产生两条相同的会话 start
    let flag = state.webchatList.webchat_todo.list.filter(item => {
      return item._id === data._id
    })
    if (data._id && flag.length === 0) { // end
      state.webchatList.webchat_todo.list.unshift(data)
      Vue.set(state.webchatList.webchat_todo, 'list', state.webchatList.webchat_todo.list)
      Vue.set(state.webchatList.webchat_todo, 'count', state.webchatList.webchat_todo.list.length)
      Vue.set(state, 'todoNum', state.webchatList.webchat_todo.list.length)
    }
  },
  //  更新在线咨询会话信息中的扩展字段
  [types.UPDATE_WEBCHAT_OTHER_PARAMS] (state, data) {
    if (state.current.webchat_todo.chatInfoById[data._id]) {
      //  如果会话没有otherParams，说明之前没有otherParams的数据，需要同时更新otherParams和firstOtherParams
      if (!state.current.webchat_todo.chatInfoById[data._id].otherParams) {
        Vue.set(state.current.webchat_todo.chatInfoById[data._id], 'firstOtherParams', data.otherParams)
      }
      Vue.set(state.current.webchat_todo.chatInfoById[data._id], 'otherParams', data.otherParams)
    }
  },
  [types.UPDATE_WEBCHAT_MESSAGE_LIST] (state, data) {
    let flag = true
    let updateArr = {}
    if (!data.lastMessage) {
      Vue.set(data, 'lastMessage', {content: '', contentType: ''})
      if (data.message) {
        Vue.set(data.lastMessage, 'content', data.message)
      }
      if (data.type) {
        Vue.set(data.lastMessage, 'type', data.type)
      }
      if (data.contentType) {
        Vue.set(data.lastMessage, 'contentType', data.contentType)
      }
    }
    state.webchatList.webchat_todo.list.forEach((item, index) => {
      if (item._id === data._id) {
        if (item._id !== state.current.webchat_todo.currentOpenSession && data.msgType !== 'newConn' && data.msgType !== 'disConn') {
          Vue.set(state.webchatList.webchat_todo.list[index], 'leakNum', state.webchatList.webchat_todo.list[index].leakNum + 1)
        }
        // 更新卡片上最后一条消息内容
        if (data.status === 'changePeer') {
          Vue.set(state.current.webchat_todo.currentSession[data._id], 'leavemsg', 'true')
          Vue.set(state.current.webchat_todo.currentSession[data._id], 'status', 'changePeer')
          Vue.set(state.webchatList.webchat_todo.list[index], 'status', data.status)
        }
        let contentType = data.lastMessage.contentType
        if (data.msgType && data.msgType === 'saveImCSRInfo' || !contentType) {
          contentType = data.msgType
        }
        // Vue.set(state.webchatList.webchat_todo.list, index, data)
        let newFlag = true
        if (data.lastMessage.contentType && data.lastMessage.contentType === 'system') {
          newFlag = false
        }
        if (data.type && data.type === 'system') {
          newFlag = false
        }
        if (data.user && data.user === 'system') {
          newFlag = false
        }
        if (newFlag) {
          if (state.webchatList.webchat_todo.list[index].lastMessage.content !== undefined) {
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'content', data.message)
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'contentType', contentType)
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'type', data.type)
          } else {
            Vue.set(state.webchatList.webchat_todo.list[index], 'lastMessage', data.message)
            Vue.set(state.webchatList.webchat_todo.list[index], 'contentType', contentType)
          }
          Vue.set(state.webchatList.webchat_todo.list[index], 'lastMessageUser', data.user)
          Vue.set(state.webchatList.webchat_todo.list[index], 'lastCustomerMsgTimeStamp', data.when)
          updateArr = state.webchatList.webchat_todo.list[index]
          state.webchatList.webchat_todo.list.splice(index, 1)
          state.webchatList.webchat_todo.list.unshift(updateArr)
        }
        flag = false
        return false
      }
    })
    // 时间检索的时候   仍然需要更新消息树中的消息
    if (state.current.webchat_todo.messageList[data._id]) {
      // 转接类信息不展示在消息列表里 修复抢接时的消息问题 2018.11.19 by yangxk
      let appendCondition = data.msgType === 'newConn' || data.msgType === 'disConn' || data.operation === 'redirect'
      if (!appendCondition) {
        state.current.webchat_todo.messageList[data._id].push(data)
        let num = Math.random() + 1
        Vue.set(state.current.webchat_todo.messageListChange, [data._id], num)   // 有新消息推进来的时候更新随机数，页面滚动到底部
        Vue.set(state.current.webchat_todo, 'messageList', state.current.webchat_todo.messageList)
      }
      if ((appendCondition && data.disConnType === 'client namespace disconnect') || (appendCondition && data.platform === 'sdk')) {
        state.current.webchat_todo.messageList[data._id].push(data)
        let num = Math.random() + 1
        Vue.set(state.current.webchat_todo.messageListChange, [data._id], num)   // 有新消息推进来的时候更新随机数，页面滚动到底部
        Vue.set(state.current.webchat_todo, 'messageList', state.current.webchat_todo.messageList)
      }
    }
    // 时间检索状态的时候  list中的数据产生了变化   因此这个地方需要判断是否在进行时间检索
    if (flag && !state.onTimeSearch) {
      state.webchatList.webchat_todo.list.unshift(data)
      Vue.set(state.webchatList.webchat_todo, 'list', state.webchatList.webchat_todo.list)
      if (data.lastMessage.contentType !== 'system' && state.webchatList.webchat_todo.list[0].lastMessage.content !== undefined) {
        Vue.set(state.webchatList.webchat_todo.list[0].lastMessage, 'content', data.message || data.lastMessage.content)
        Vue.set(state.webchatList.webchat_todo.list[0].lastMessage, 'type', data.type)
        Vue.set(state.webchatList.webchat_todo.list[0], 'lastMessageUser', data.user)
        Vue.set(state.webchatList.webchat_todo.list[0].lastMessage, 'contentType', data.lastMessage.contentType)
      } else if (data.lastMessage.contentType !== 'system' && !state.webchatList.webchat_todo.list[0].lastMessage.content) {
        Vue.set(state.webchatList.webchat_todo.list[0], 'lastMessage', data.message || data.lastMessage.content)
        Vue.set(state.webchatList.webchat_todo.list[0], 'lastMessageUser', data.user)
        Vue.set(state.webchatList.webchat_todo.list[0], 'contentType', data.lastMessage.contentType)
        Vue.set(state.webchatList.webchat_todo.list[0], 'type', data.lastMessage.type)
      }
      Vue.set(state.webchatList.webchat_todo, 'count', state.webchatList.webchat_todo.list.length)
      Vue.set(state, 'todoNum', state.webchatList.webchat_todo.list.length)
      // Vue.set(state.webchatList.webchat_todo.list[0], 'leakNum', state.webchatList.webchat_todo.list[0].leakNum + 1)
    }
  },
  [types.AUTO_CUST_INFO] (state, {sessionId, data}) {
    Vue.set(state.autoCust, sessionId, data)
  },
  /*
 * 切换路由之后，保存上次输入的内容
 * */
  [types.REMEMBER_LAST_CON] (state, {id, data}) {
    Vue.set(state.lastContent, id, data)
  },
  [types.UBAINFO] (state, { _id, page, lastPageTimeStart, sessionId, pages, allPages }) {
    let ubaInfo = state.ubaInfoList[sessionId]
    if (ubaInfo) {
      if (page === 1) {
        ubaInfo.pages = []
      }
      ubaInfo.page = page
      ubaInfo.lastPageTimeStart = lastPageTimeStart
      ubaInfo.pages = ubaInfo.pages.concat(pages).slice(0, page * 10)
      ubaInfo.count = state.ubaInfoListAll[sessionId].pages.length
    } else {
      ubaInfo = {
        _id, page, lastPageTimeStart, pages, count: pages.length
      }
      state.ubaInfoList[sessionId] = ubaInfo
      state.ubaInfoListAll[sessionId] = deepClone(ubaInfo)
      ubaInfo.pages = ubaInfo.pages.slice(0, ubaInfo.page * 10)
    }
  },
  [types.UBAINFO_PUSH] (state, page) {
    let ubaInfo = state.ubaInfoList[page.sessionId]
    // let ubaInfo = state.ubaInfoList[page.sessionId]
    let allUbaInfo = state.ubaInfoListAll[page.sessionId]
    if (page.change) {
      let changePage = ubaInfo.pages.filter(obj => obj._id === page._id)[0]
      Vue.set(changePage, 'stayTimeDesc', page.stayTimeDesc)
    } else {
      ubaInfo.pages.unshift(page)
      allUbaInfo.pages.unshift(page)
      Vue.set(ubaInfo, 'count', allUbaInfo.pages.length)
    }
  },
  [types.UPDATE_PRELOADED_MSG] (state, {message, flag, _id}) { // 访客消息预加载
    let messageNew = filterXSS(decodeURIComponent(message)) || ''
    if (messageNew === 'undefined') {
      messageNew = ''
    }
    if (messageNew === '') {
      flag = false
    }
    if (state.preload[_id]) {
      Vue.set(state.preload[_id], 'preloadedMsgCon', messageNew)
      Vue.set(state.preload[_id], 'preloadedFlag', flag)
    }
  },
  [types.UPDATE_STARTWAITTIMER] (state, {startWaitTimer, sessionId}) {
    state.webchatList.webchat_todo.list.forEach((item, index) => {
      if (sessionId === item._id) {
        console.log(state.webchatList.webchat_todo.list[index].startWaitTimerStamp, state.webchatList.webchat_todo.list[index].startWaitTimer, startWaitTimer)
        if (state.webchatList.webchat_todo.list[index].startWaitTimerStamp &&
          state.webchatList.webchat_todo.list[index].startWaitTimer > -1 &&
          startWaitTimer > -1) {
          return
        }
        let now = new Date().getTime()
        Vue.set(state.webchatList.webchat_todo.list[index], 'startWaitTimer', startWaitTimer)
        Vue.set(state.webchatList.webchat_todo.list[index], 'startWaitTimerStamp', now)
      }
    })
  },
  [types.CHANGE_LOAD_STATUS] (state, data) {
    Vue.set(state, 'load', true)
  },
  /**
   * 绑定客户后更新webchatlist客户信息
   * @param state
   * @param message
   * @param flag
   */
  [types.UPDATE_WEBCHAT_RELATION_CUSTOMER] (state, {sid, customer}) {
    // 更新在线咨询模块客户信息
    for (let webchatTab in state.current) {
      if (webchatTab === 'webchat_todo') {
        for (let _id in state.current.webchat_todo.currentSession) {
          let session = state.current.webchat_todo.currentSession[_id]
          if (session.sid === sid) {
            session.custId = customer.custId
            Vue.set(state.current.webchat_todo.currentSession[_id], 'sName', customer.name)
            Vue.set(state.current.webchat_todo.currentSession[_id], 'cust_status', customer.status)
            Vue.set(state.current.webchat_todo.currentSession[_id], 'custId', customer._id)
            Vue.set(state.current.webchat_todo.currentSession[_id], 'currentCustomer', customer)
            if (customer._id) {
              Vue.set(state.current.webchat_todo, 'currentCustomer', customer)
            } else {
              Vue.set(state.current.webchat_todo, 'currentCustomer', {})
            }
          }
        }
      } else if (webchatTab === 'webchat_all' || webchatTab === 'webchat_robot' || webchatTab === 'webchat_laterProcess' || webchatTab === 'webchat_queue') {
        if (state.current[webchatTab].currentSession) {
          let session = state.current[webchatTab].currentSession
          if (session.sid === sid) {
            session.custId = customer.custId
            Vue.set(state.current[webchatTab].currentSession, 'sName', customer.name)
            Vue.set(state.current[webchatTab].currentSession, 'cust_status', customer.status)
            Vue.set(state.current[webchatTab].currentSession, 'custId', customer._id)
            Vue.set(state.current[webchatTab].currentSession, 'currentCustomer', customer)
            if (customer._id) {
              Vue.set(state.current[webchatTab], 'currentCustomer', customer)
            } else {
              Vue.set(state.current[webchatTab], 'currentCustomer', {})
            }
          }
        }
      }
    }
    // 更新在线咨询模块列表客户名称
    for (let webchatTab in state.webchatList) {
      state.webchatList[webchatTab].list && state.webchatList[webchatTab].list.forEach(webchatInfo => {
        if (webchatInfo.sid === sid) {
          if (customer.name) {
            // webchatInfo.sName = customer.name
            Vue.set(webchatInfo, 'sName', customer.name)
          }
          Vue.set(webchatInfo, 'custId', customer._id)
          Vue.set(webchatInfo, 'cust_status', customer.status)
        }
      })
    }
  },
  // 点击搜索到的文本复制到发送框
  [types.COPY_IMSEARCH_VALUE] (state, data) {
    state.transCache.copyImSearchValue.value = data.value
    state.transCache.copyImSearchValue.random = data.random
  },
  // 保存自动定位到客户时的电话号码
  [types.SET_AUTOCUSTPHONE_VAL] (state, {sessionId, data}) {
    Vue.set(state.autoCust[sessionId], 'autoCustPhone', data)
  },
  // 保存自动定位到客户时的客户名称
  [types.SET_AUTOCUSTNAME_VAL] (state, {sessionId, data}) {
    Vue.set(state.autoCust[sessionId], 'autoCustName', data)
  },
  // 保存自动定位到客户时的联系人
  [types.SET_AUTOCUSTPERSON_VAL] (state, {sessionId, data}) {
    Vue.set(state.autoCust[sessionId], 'autoContectPerson', data)
  },
  // 保存自动定位到客户时的匹配内容
  [types.SET_AUTOMATCHCONTENT_VAL] (state, {sessionId, data}) {
    Vue.set(state.autoCust[sessionId], 'autoMatchContent', data)
    Vue.set(state.autoCust[sessionId], 'autoMatchType', 'userset')
  },
  [types.SET_OFFORONLINE_STATUS] (state, {_id, statu}) {
    let num = 0
    let flag = false
    for (let i = 0; i < state.webchatList.webchat_todo.list.length; i++) {
      if (state.webchatList.webchat_todo.list[i]._id === _id) {
        num = i
        flag = true
      }
    }
    if (flag) {
      Vue.set(state.webchatList.webchat_todo.list[num], 'userStatus', statu)
    }
  },
  [types.UPDATELASTMESSAGE] (state, data) {
    let message = state.current.webchat_todo.messageList[data._id] || []
    if (message && message.length > 0) {
      Vue.set(state.current.webchat_todo.messageList[data._id][message.length - 1], 'dealCustomerMsg', true)
    }
  },
  [types.UPDATEDEALMSGSTATUS] (state, data) {
    let message = state.current.webchat_todo.messageList[data.sessionId] || []
    if (message && message.length > 0) {
      message.forEach((item, index) => {
        Vue.set(item, 'dealUserMsg', true)
      })
    }
  },
  [types.UPDATE_WEBCHAT_SESSION_REMARK] (state, data) {
    let sessionList = state.webchatList.webchat_todo.list
    sessionList.forEach((item, index) => {
      if (state.webchatList.webchat_todo.list[index]._id === data._id) {
        Vue.set(state.webchatList.webchat_todo.list[index], 'remark', data.content)
      }
    })
    if (state.current.webchat_todo.currentSession[data._id]) {
      Vue.set(state.current.webchat_todo.currentSession[data._id], 'remark', data.content)
    }
  },
  [types.SET_CHAT_VOICE_PLAY] (state, data) {
    if (data.tabType === 'webchat_todo') {
      if (data.flag === 'user') {
        state.current.webchat_todo.messageList[data._id] && state.current.webchat_todo.messageList[data._id].forEach(item => {
          if ((item.contentType === 'voice' || (item.contentType === 'file' && item.mp3Suffix)) && item.when === data.when) {
            Vue.set(item, 'play', data.data)
          }
        })
      } else if (data.flag === 'history') {
        state.current.webchat_todo.historyMsgList[data._id] && state.current.webchat_todo.historyMsgList[data._id].forEach(item => {
          if ((item.contentType === 'voice' || (item.contentType === 'file' && item.mp3Suffix)) && item.when === data.when) {
            Vue.set(item, 'play', data.data)
          }
        })
      }
    } else if (data.tabType === 'webchat_all' || data.tabType === 'webchat_robot' || data.tabType === 'webchat_laterProcess' || data.tabType === 'webchat_queue') {
      if (data.flag === 'user') {
        state.current[data.tabType].messageList[data._id] && state.current[data.tabType].messageList[data._id].forEach(item => {
          if ((item.contentType === 'voice' || (item.contentType === 'file' && item.mp3Suffix)) && item.when === data.when) {
            Vue.set(item, 'play', data.data)
          }
        })
      } else if (data.flag === 'history') {
        state.current[data.tabType].historyMsgList[data._id] && state.current[data.tabType].historyMsgList[data._id].forEach(item => {
          if ((item.contentType === 'voice' || (item.contentType === 'file' && item.mp3Suffix)) && item.when === data.when) {
            Vue.set(item, 'play', data.data)
          }
        })
      }
    }
  },
  [types.VOICETOTEXT] (state, data) {
    state.current[data.tabType].messageList[data._id] && state.current[data.tabType].messageList[data._id].forEach(item => {
      if ((item.contentType === 'voice' || (item.contentType === 'file' && item.mp3Suffix)) && item.when === data.when) {
        Vue.set(item, 'isFinish', data.isFinish)
        Vue.set(item, 'voiceMsg', data.voiceMsg)
      }
    })
  },
  [types.RESET_USER_HISTORY_VOICE_PLAY] (state, data) {
    if (data.tabType === 'webchat_todo') {
      state.current.webchat_todo.messageList[data._id] && state.current.webchat_todo.messageList[data._id].forEach(item => {
        if (item.contentType === 'voice' || (item.contentType === 'file' && item.mp3Suffix)) {
          Vue.set(item, 'play', false)
        }
      })
      state.current.webchat_todo.historyMsgList[data._id] && state.current.webchat_todo.historyMsgList[data._id].forEach(itemAll => {
        if (itemAll.contentType === 'voice' || (itemAll.contentType === 'file' && itemAll.mp3Suffix)) {
          Vue.set(itemAll, 'play', false)
        }
      })
    } else if (data.tabType === 'webchat_all' || data.tabType === 'webchat_robot' || data.tabType === 'webchat_laterProcess' || data.tabType == 'webchat_queue') {
      state.current[data.tabType].messageList[data._id] && state.current[data.tabType].messageList[data._id].forEach(item => {
        if (item.contentType === 'voice' || (item.contentType === 'file' && item.mp3Suffix)) {
          Vue.set(item, 'play', false)
        }
      })
      state.current[data.tabType].historyMsgList[data._id] && state.current[data.tabType].historyMsgList[data._id].forEach(itemAll => {
        if (itemAll.contentType === 'voice' || (itemAll.contentType === 'file' && itemAll.mp3Suffix)) {
          Vue.set(itemAll, 'play', false)
        }
      })
    }
  },
  [types.UPDATE_TIME_SEARCH_STATE] (state, data) {
    state.onTimeSearch = data
  },
  [types.UPDATE_PRELOADED_MSG_FLAG] (state, data) {
    if (state.preload[data._id]) {
      Vue.set(state.preload[data._id], 'preloadedFlag', data.value)
    }
  },
  // 更新视频邀请状态
  [types.UPDATE_VIDEO_INVITE_STATUS] (state, data) {
    state.isinviteVideo = data
  },
  // 当前视频的发起人
  [types.UPDATE_VIDEO_INVITE_PERSONNEL] (state, data) {
    state.invitePersonnel = data
  },
  // 是否隐藏视频摄像头
  [types.UPDATE_HIDE_VIDEO_CAMERA_STATU] (state, data) {
    state.hideVideoCamera = data
  },
  // 更新视频邀请信息
  [types.UPDATE_VIDEO_INVITE_DATA] (state, data) {
    state.videoInvitedData.videoInviteSessionid = data.videoInviteSessionid
    state.videoInvitedData.videoInviteToken = data.videoInviteToken
    state.videoInvitedData.platform = data.platform
    state.videoInvitedData.roomId = data.roomId || ''
    state.videoInvitedData.password = data.password || ''
    state.videoInvitedData.videoType = data.videoType || ''
    if (data.videoType && data.videoType === 'voice') {
      state.hideVideoCamera = true
    }
  },
  // 更新视频聊天状态
  [types.UPDATE_VIDEO_CHAT_STATUS] (state, data) {
    state.isVideoing = data
    if (!data) {
      // 视频之后，需要将隐藏摄像头的状态至为false
      state.hideVideoCamera = false
    }
  },
  // 更新视频聊天信息
  [types.UPDATE_VIDEO_CHATDATA] (state, data) {
    state.videoChatData.videoSessionid = data.videoSessionid
    state.videoChatData.videoUsername = data.videoUsername
    // state.videoChatData.videoToken = data.videoToken
    state.videoChatData.videoSource = data.videoSource
    state.videoChatData.roomId = data.roomId
    state.videoChatData.password = data.password
    if (data.videoType && data.videoType === 'voice') {
      state.hideVideoCamera = true
    }
  },
  // 视频挂断
  [types.VIDEOHANGUP] (state, data) {
    state.videoChatData.refreshFlag = data
  },
  // 视频正常挂断后更新card视频图标信息
  [types.UPDATE_SHOWVIDEOICON] (state, data) {
    state.webchatList.webchat_todo.list.forEach((item, index) => {
      if (data.videoSessionid === item._id) {
        if (!item.showVideoIcon) {
          Vue.set(state.webchatList.webchat_todo.list[index], 'showVideoIcon', true)
        }
      }
    })
  },
  [types.START_MOVIE] (state) {
    state.trackMovie.isPlaying = true
  },
  [types.STOP_MOVIE] (state) {
    state.trackMovie.isPlaying = false
  },
  [types.INIT_PLAYER] (state, result) {
    if (result.err) {
      console.log(result.err)
    } else {
      state.trackMovie.player = result.data
    }
  },
  [types.UPDATE_PLAY_SPEED] (state, result) {
    if (result.err) {
      console.log(result.err)
    } else {
      state.trackMovie.speed = result.data
    }
  },
  [types.UPDATE_CURRENT_MOVIE] (state, result) {
    if (result.err) {
      console.log(result.err)
    } else {
      state.trackMovie.movie = result.data
    }
  },
  [types.UPDATE_WEBCHAT_INVITECONDATA] (state, {data, value}) {
    Vue.set(state.current.webchat_todo.currentSession[data.sessionId], 'inviteCon', value)
    Vue.set(state.current.webchat_todo.currentSession[data.sessionId], 'inviteConData', data)
  },
  [types.CANCEL_WEBCHAT_INVITECONDATA] (state, {sessionId, value}) {
    Vue.set(state.current.webchat_todo.currentSession[sessionId], 'inviteCon', value)
    Vue.set(state.current.webchat_todo.currentSession[sessionId], 'inviteConData', {})
  },
  [types.SET_WEBCHAT_MORE] (state, {activeName, more}) {
    state.webchatList[activeName].more = more
    // state.webchatList[activeName].count = count
  },
  /*
   * 切换卡片的时候，保存当前按钮的存在的状态用来做上传组件重置的判断
   * */
  [types.SET_CHAT_PLATFORM] (state, data) {
    Vue.set(state.current.webchat_todo, 'previousSessionPlatform', data)
  },
  /*
   * 设置当前时间和服务器时间的时间差
   * */
  [types.SET_SERVER_TIME_DIFF] (state, data) {
    Vue.set(state, 'nowTimeDiff', data)
  },
  /*
   * 撤回的消息未找到，删除本地消息
   * */
  [types.DELETE_MESSAGE] (state, data) {
    let lastMessageFlag = false
    if (state.current.webchat_todo.messageList[data.sesssionId]) {
      state.current.webchat_todo.messageList[data.sesssionId].forEach((item, index) => {
        if (item._id === data._id) {
          let num = parseInt(deepClone(state.current.webchat_todo.messageList[data.sesssionId].length)) - 1
          if (num === index) {
            lastMessageFlag = true
          }
          state.current.webchat_todo.messageList[data.sesssionId].splice(index, 1)
        }
      })
    }
    if (lastMessageFlag) {
      state.webchatList.webchat_todo.list.forEach((item, index) => {
        if (item._id === data._id && (data.when === item.lastMessageWhen || data.when === item.lastMessage.lastMessageWhen)) {
          if (state.webchatList.webchat_todo.list[index].lastMessage.content !== undefined) {
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'content', '')
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'contentType', 'text')
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'lastMessageWhen', 0)
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'type', 0)
          } else {
            Vue.set(state.webchatList.webchat_todo.list[index], 'lastMessage', '')
            Vue.set(state.webchatList.webchat_todo.list[index], 'contentType', 'text')
            Vue.set(state.webchatList.webchat_todo.list[index], 'lastMessageWhen', 0)
          }
        }
      })
    }
  },
  /*
   * 坐席消息撤回功能
   * */
  [types.SHIELD_MSG] (state, {data, message, flag}) {
    let sesssionId = data.sessionId
    let when = data.when || 0
    if (flag) {
      sesssionId = data._id
    }
    let obj = {
      type: 'system',
      platform: data.platform,
      sid: data.sid,
      createTime: data.createTime,
      message: message,
      _id: sesssionId
    }
    // 自己撤回的消息，socket推送的时候，会带上syncUser字段，消息撤回成功的接口，也加上了syncUser字段
    // if (data.contentType === 'text' && data.syncUser) {
    //   if (data.message) {
    //     let msgCon = data.message
    //     let reg = new RegExp(`<strike>|<font|<file>|aidev2`, 'g')
    //     if (data.content && !reg.test(msgCon)) {
    //       obj.drawMessage = data.content
    //       obj.msgType = 'withdrawMessage'
    //     }
    //   }
    //   let now = new Date().getTime()
    //   obj.drawTime = now
    // }
    let lastMessageFlag = false
    if (state.current.webchat_todo.messageList[sesssionId]) {
      state.current.webchat_todo.messageList[sesssionId].forEach((item, index) => {
        let messageId = data._id
        if (flag) {
          messageId = data.messageId
        }
        if (item.messageId === messageId || item._id === messageId) {
          let num = parseInt(deepClone(state.current.webchat_todo.messageList[sesssionId].length)) - 1
          if (num === index) {
            lastMessageFlag = true
          }
          obj.messageId = messageId
          if (data.contentType === 'text' && data.syncUser) {
            if (data.message) {
              let msgCon = data.message
              let reg = new RegExp(`<strike>|<font|<file>|aidev2`, 'g')
              if (data.content && !reg.test(msgCon)) {
                obj.drawMessage = data.content
                obj.msgType = 'withdrawMessage'
              } else if (item.msgType === 'withdrawMessage') {
                obj.drawMessage = item.drawMessage
                obj.msgType = 'withdrawMessage'
              }
            }
            let now = new Date().getTime()
            obj.drawTime = now
          }
          if (item.aiContentText) {
            Vue.set(obj, "aiContentText", filterXSS(item.aiContentText))
          }
          state.current.webchat_todo.messageList[sesssionId].splice(index, 1, obj)
        }
      })
    }
    if (lastMessageFlag) {
      state.webchatList.webchat_todo.list.forEach((item, index) => {
        if (item._id === obj._id && (data.when === item.lastMessageWhen || data.when === item.lastMessage.lastMessageWhen)) {
          if (state.webchatList.webchat_todo.list[index].lastMessage.content !== undefined) {
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'content', obj.message)
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'contentType', 'text')
            Vue.set(state.webchatList.webchat_todo.list[index].lastMessage, 'lastMessageWhen', when)
          } else {
            Vue.set(state.webchatList.webchat_todo.list[index], 'lastMessage', obj.message)
            Vue.set(state.webchatList.webchat_todo.list[index], 'contentType', 'text')
            Vue.set(state.webchatList.webchat_todo.list[index], 'lastMessageWhen', when)
          }
        }
      })
    }
  },
  // 设置屏幕分享状态及id
  [types.SET_SHARESCREEN] (state, {status, id, name}) {
    state.isShareScreening = status
    state.shareScreenSessionid = id
    state.shareScreenCustName = name
  },
  // 未处理列表排序
  [types.SORTLISTBY_TYPE] (state) {
    let list = state.webchatList.webchat_todo.list || []
    let userSessionId = state.currentUser._id
    // 分开置顶与非置顶
    let topList = list.filter((item) => {
      return item.setTopStatus && item.setTopAgent && item.setTopAgent.indexOf(userSessionId) > -1
    })
    let commonList = list.filter((item) => {
      return !(item.setTopStatus && item.setTopAgent && item.setTopAgent.indexOf(userSessionId) > -1)
    })
    topList = orderBy(topList, [(o) => {
      return o.setTopTime || 0
    }], ['desc'])
    // 非置顶数组排序
    let type = state.webchatList.sortType || 'lastCustomerMsgTimeStamp'
    let accountList = []
    try {
      accountList = JSON.parse(window.localStorage.getItem('localCacheArray'))
    } catch (error) {
      console.log(error)
    }
    const customerAccount = ['T00000000003', 'T00000014316', 'T00000015316']
    if (customerAccount.some(i => accountList.includes(i))) { // 由于在客户环境下才能完全复现，22.4只针对三个账号进行排序修复，后续如果客户那边不再复现把判断去掉
      commonList = commonList.map(i => ({
        ...i,
        lastCustomerMsgTimeStamp: i.lastCustomerMsgTimeStamp ||
          (i.beginTime ? new Date(i.beginTime).getTime() : new Date(i.createTime).getTime())
      }))
    }
    commonList = orderBy(commonList, [(o) => {
      // 优先按照选择的方式排
      let aa = (o[type] || o['lastClaimTime']) || 0
      if (type === 'startWaitTimerStamp') {
        let now = new Date().getTime()
        let aTimer
        if (o['startWaitTimer'] > -1) {
          aTimer = o['startWaitTimer']
        } else {
          aTimer = -1
        }
        console.log(aa, aTimer)
        aa = (aa && aTimer > -1) ? (now - aa) / 1000 + aTimer : -1
      }
      if (type === 'beginTime') {
        aa = aa ? new Date(aa).getTime() : 0
      }
      return aa
    }, (o) => {
      // 如果没有对应值，按接入时间排序
      let aa = o['beginTime'] || 0
      return aa
    }], ['desc', 'desc'])
    list = topList.concat(commonList)
    Vue.set(state.webchatList.webchat_todo, 'list', list)
  },
  // 设置未处理列表排序类型
  [types.SET_SORTTYPE] (state, {type}) {
    Vue.set(state.webchatList, 'sortType', type)
  },
  // 点击卡片加载右侧数据
  [types.RENDER_WEBCHAT_CONTAINER] (state, data) {
    Vue.set(state, 'renderWebchatContainer', data)
  },
  // 将卡片的data放置到右侧详情的数据中
  [types.SER_CARD_DATA_TO_CONTAINER] (state, data) {
    Vue.set(state.current.webchat_todo.chatInfoById, data._id, data)
  },
  // 切换机器人辅助坐席开关
  [types.CHANGE_M7_AIROBOT_STATUS] (state, status) {
    if (state.currentUser) {
      Vue.set(state.currentUser, 'robotAssistStatus', status)
    }
  },
  // 改变输入框的高度
  [types.CHANGE_INPUT_HEIGHT] (state, data) {
    Vue.set(state.webchatList, 'inputHeight', data)
  },
  // 辅助机器人回答
  [types.M7AIROBOT_ANSWERS] (state, data) {
    let sessionId = data.sessionId
    console.log(data.list)
    if (state.webchatList.webchat_todo.init) {
      state.webchatList.webchat_todo.list.forEach((item, index) => {
        if (item._id === sessionId) {
          Vue.set(state.webchatList.webchat_todo.list[index], 'm7RobotAnswers', data.list)
        }
      })
    }
  },
  // 刷新在线咨询会话列表上限
  [types.REFRESH_IM_DEAL_COUNT] (state, data) {
    Vue.set(state.currentUser, ['imDealCount'], data)
  },
  [types.CLOSESESSIONSTATUS] (state, data) {
    Vue.set(state.current[data.tabType].currentSession[data._id], 'closeSessionList', data.list)
  },
  // 刷新消息的发送状态
  [types.UPDATE_SEND_STATUS_FAIL] (state, {data, index}) {
    Vue.set(state.current.webchat_todo.messageList[data.sessionId][index], 'sendStatus', 'fail')
  },
  // 更新待处理中的会话稍后处理状态
  [types.UPDATE_LATERPROCESS_STATUS] (state, data) {
    state.webchatList.webchat_todo.list.forEach((item, index) => {
      if (data._id === item._id) {
        Vue.set(state.webchatList.webchat_todo.list[index], 'laterProcess', data.status)
      }
    })
    if (state.current.webchat_todo.currentSession[data._id]) {
      Vue.set(state.current.webchat_todo.currentSession[data._id], 'laterProcess', data.status)
    }
  },
  // 更新主动提交质检状态
  [types.UPDATE_CHECKUP_STATUS] (state, data) {
    console.log('1212121211seee', data)
    console.log(state.webchatList.webchat_all.list)
    state.webchatList.webchat_all.list.forEach((item, index) => {
      if (data._id === item._id) {
        Vue.set(state.webchatList.webchat_all.list[index], 'applyQcWebchat', data.status)
        console.log(state.webchatList.webchat_all.list[index])
      }
    })
    if (state.current.webchat_all.currentSession._id === data._id) {
      Vue.set(state.current.webchat_all.currentSession, 'applyQcWebchat', data.status)
    }
  },
  // 更新待处理列表中的会话置顶状态
  [types.UPDATE_SETTOP_STATUS] (state, data) {
    state.webchatList.webchat_todo.list.forEach((item, index) => {
      if (data._id === item._id) {
        if (data.status === false) {
          // 如果是取消置顶，移除置顶座席数组内的自己的sessionId
          if (state.webchatList.webchat_todo.list[index].setTopAgent) {
            if (state.webchatList.webchat_todo.list[index].setTopAgent.indexOf(data.userSessionId) !== -1) {
              let index1 = state.webchatList.webchat_todo.list[index].setTopAgent.indexOf(data.userSessionId)
              state.webchatList.webchat_todo.list[index].setTopAgent.splice(index1, 1)
            }
          }
        } else {
          // 更新置顶时间
          Vue.set(state.webchatList.webchat_todo.list[index], 'setTopTime', new Date().getTime())
          // 更新置顶座席数组
          if (state.webchatList.webchat_todo.list[index].setTopAgent) {
            if (state.webchatList.webchat_todo.list[index].setTopAgent.indexOf('') === -1) {
              state.webchatList.webchat_todo.list[index].setTopAgent.push(data.userSessionId)
            }
          } else {
            Vue.set(state.webchatList.webchat_todo.list[index], 'setTopAgent', [])
            state.webchatList.webchat_todo.list[index].setTopAgent.push(data.userSessionId)
          }
        }
        // 更新置顶状态
        Vue.set(state.webchatList.webchat_todo.list[index], 'setTopStatus', data.status)
      }
    })
  },
  // 更新表情包数量
  [types.UPDATE_EMOTICONNUMBER] (state, data){
    Vue.set(state, 'emoticonNumber', data)
  },
  // 增加当前会话所获取到的线索
  [types.UPDATE_SESSION_CUE] (state, data){
    if (data.type === 'webchatList') {
      Vue.set(state.webchatList[data.todoType].list[data.index], 'cue', data.data)
    } else if (data.type === 'current') {
      Vue.set(state.current[data.todoType].currentSession[data._id], 'cue', data.data)
    }
  },
  // 
  [types.UPDATE_WEBCHAT_FORM_LIST] (state, data){
    Vue.set(state, 'sessionFormList', data)
  }
}

